package com.mfs.proxy.core.client.config;

import com.mfs.common.constant.Constant;
import com.mfs.common.utills.StringUtil;
import com.mfs.proxy.core.client.processor.DefaultProxyProcessor;
import com.mfs.proxy.core.client.processor.Transfer;
import com.mfs.proxy.core.constant.AppConstant;
import com.mfs.proxy.core.exceptions.ProxyException;
import com.mfs.proxy.core.server.endpoint.ProxyHandle;
import lombok.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.net.URISyntaxException;
import java.util.Properties;

/**
 * 代理客户端配置信息
 *
 * @author mfs
 * @date 2023/7/1 16:09
 **/
@Data
public class ClientConfig implements Serializable {
    private final static String NAMESPACE = Constant.MFS + Constant.DOT + AppConstant.APP_NAME;
    /**
     * 代理服务端IP
     * */
    private String serverIp;
    /**
     * 代理服务端端口
     * */
    private Integer serverPort;
    /**
     * 目标服务地址
     * */
    private String targetIp;

    /**
     * 目标服务端口
     * */
    private Integer targetPort;

    /**
     * 目标服务名
     * */
    private String targetName;

    /**
     * 备注
     * */
    private String describe;

    /**
     *
     * 代理句柄，成功建立代理后，代理服务端返回
     */
    private ProxyHandle proxyHandle;

    /**
     * 最大请求并发数
     * 代理请求线程池的大小，默认CPU核数
     * */
    private Integer concurrentRequestNum;

    /**
     * 代理请求转换器
     * */
    private transient Class<? extends Transfer> requestTransferClass;

    /**
     * 代理请求转换器
     * */
    private transient Class<? extends Transfer> responseTransferClass;



    public static ClientConfig getInstance() {
        return InstanceHolder.clientConfig;
    }

    private ClientConfig() {
        try {
            String proxyHome = System.getenv("PROXY_HOME");
            proxyHome = proxyHome.endsWith("/") ? proxyHome : proxyHome + "/";
            File file = new File(proxyHome + "client/config/client.properties");
            Properties configProperties = new Properties();
            configProperties.load(new FileInputStream(file));
            serverIp = configProperties.getProperty(NAMESPACE + ".server.ip");
            serverPort = Integer.valueOf(configProperties.getProperty(NAMESPACE + ".server.port"));
            targetIp = configProperties.getProperty(NAMESPACE + ".target.ip");
            targetPort = Integer.valueOf(configProperties.getProperty(NAMESPACE + ".target.port"));
            targetName = configProperties.getProperty(NAMESPACE + ".target.name");
            describe = configProperties.getProperty(NAMESPACE + ".target.describe");
            concurrentRequestNum = Integer.valueOf(configProperties.getProperty(NAMESPACE + ".client.concurrent.request.num", Integer.toString(Runtime.getRuntime().availableProcessors())));
            String requestTransfer = configProperties.getProperty(NAMESPACE + ".client.request.transfer");
            requestTransferClass = StringUtil.isNotBlank(requestTransfer) ? (Class<? extends Transfer>) Class.forName(requestTransfer, true, Thread.currentThread().getContextClassLoader()) : null;
            String responseTransfer = configProperties.getProperty(NAMESPACE + ".client.response.transfer");
            responseTransferClass = StringUtil.isNotBlank(responseTransfer) ? (Class<? extends Transfer>) Class.forName(responseTransfer, true, Thread.currentThread().getContextClassLoader()) : null;
        } catch (Exception e) {
           throw new ProxyException("配置读取失败", e);
        }
    }

    static class InstanceHolder {
        private static ClientConfig clientConfig = new ClientConfig();
    }
}
